Skip to content

feat(transport): Add EnvelopePrinterTransport for debug logging#6181

Open
ericapisani wants to merge 8 commits intomasterfrom
envelope-printer-transport-zcgvd
Open

feat(transport): Add EnvelopePrinterTransport for debug logging#6181
ericapisani wants to merge 8 commits intomasterfrom
envelope-printer-transport-zcgvd

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented May 1, 2026

Adds EnvelopePrinterTransport, a decorator transport that wraps the real transport and pretty-prints each envelope's headers and item payloads to the SDK debug logger before forwarding the envelope.

Enabled via SENTRY_PRINT_ENVELOPES=1 (also accepts true/yes). When unset or falsy, no wrapping occurs and there's no runtime cost. Useful for local debugging without having to run a local Sentry instance or intercept network traffic.

Fixes PY-2398
Fixes #6183

ericapisani and others added 2 commits May 1, 2026 10:38
Add a decorator transport that logs envelope contents via the SDK
debug logger before forwarding to the inner transport. Activated by
setting SENTRY_PRINT_ENVELOPES=1|true|yes.

Includes tests for delegation, logging behavior, make_transport
integration, and strict env var parsing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 7.38s

All tests are passing successfully.

❌ Patch coverage is 46.00%. Project has 14962 uncovered lines.

Files with missing lines (1)
File Patch % Lines
transport.py 25.48% ⚠️ 430 Missing and 7 partials

Generated by Codecov Action

@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 1, 2026

@ericapisani
Copy link
Copy Markdown
Member Author

bugbot run

@ericapisani
Copy link
Copy Markdown
Member Author

@sentry review

Comment thread sentry_sdk/transport.py
Comment thread sentry_sdk/transport.py
Comment thread sentry_sdk/transport.py Outdated
…sport coverage

- Override __class__ to delegate to inner transport so isinstance checks
  (e.g. AsyncHttpTransport) remain transparent
- Add __getattr__ fallback for attributes not explicitly defined
- Return values from flush() and kill() so async tasks propagate
- Wrap transport in all make_transport paths (pre-instantiated, callable)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ericapisani
Copy link
Copy Markdown
Member Author

bugbot run

@ericapisani
Copy link
Copy Markdown
Member Author

@sentry review

Comment thread sentry_sdk/transport.py
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit f050b85. Configure here.

Comment thread sentry_sdk/transport.py
@ericapisani ericapisani marked this pull request as ready for review May 4, 2026 14:51
@ericapisani ericapisani requested a review from a team as a code owner May 4, 2026 14:51
Comment thread sentry_sdk/transport.py
Comment on lines +1089 to +1092
def __init__(self, transport: "Transport") -> None:
Transport.__init__(self, options=transport.options)
self._inner = transport
self.parsed_dsn = transport.parsed_dsn
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: A KeyError can occur in _EnvelopePrinterTransport when wrapping a custom transport that has a non-empty options dictionary without a "dsn" key.
Severity: LOW

Suggested Fix

In Transport.__init__, modify the condition to safely check for the existence of the "dsn" key before accessing it, for example, by using options.get("dsn") or "dsn" in options.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry_sdk/transport.py#L1089-L1092

Potential issue: The `_EnvelopePrinterTransport` wraps a given transport and calls the
base `Transport.__init__` with the inner transport's `options`. The base `__init__`
method then executes `if options and options["dsn"]`. If a user provides a custom
transport with a non-empty `options` dictionary that lacks a `"dsn"` key, this check
will raise a `KeyError`. This bug is triggered when the `SENTRY_PRINT_ENVELOPES=1`
environment variable is set and a pre-instantiated custom transport is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add transport for pretty-printing envelope contents when debugging span-first logic

1 participant